﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OLMS.DAL;

namespace OLMS.BLL
{
    public class CartBLL
    {
        //获取购物车的音乐专辑列表
        public static List<Carts> GetCarts(int userId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Carts.Include("Albums").Where(c => c.UserId == userId).ToList();
            }
        }

        //添加到购物车
        public static bool AddToCart(int userId, int albumId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                Carts carts = db.Carts.SingleOrDefault(c => c.UserId == userId && c.AlbumId == albumId);
                if (carts != null)
                {
                    //如果音乐专辑存在，则音乐专辑购买数量加1
                    carts.Count++;
                }
                else
                {
                    //向购物车添加新音乐专辑并且购买数量为1
                    carts = new Carts();
                    carts.UserId = userId;
                    carts.AlbumId = albumId;
                    carts.Count = 1;
                    carts.DateCreated = DateTime.Now;
                    db.Carts.Add(carts);
                }

                return db.SaveChanges() > -1 ? true : false;
            }
        }
        //从购物车中移除音乐专辑
        public static bool RemoveFromCart(int userId, int cartId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                Carts carts = db.Carts.SingleOrDefault(c => c.UserId == userId && c.CartId == cartId);
                if (carts != null)
                {
                    if (carts.Count > 1)
                    {
                        carts.Count--;
                    }
                    else
                    {
                        db.Carts.Remove(carts);
                    }
                    return db.SaveChanges() > -1 ? true : false;
                }
                else
                {
                    return false;
                }

            }
        }
        //清空购物车
        public static void EmptyCart(int userId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                List<Carts> cartsList = db.Carts.Where(c => c.UserId == userId).ToList();
                foreach (Carts carts in cartsList)
                {

                    db.Carts.Remove(carts);
                    db.SaveChanges();
                }
            }
        }
        //获取购物车中音乐专辑数量
        public static int GetCount(int userId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Carts.Where(c => c.UserId == userId).Select(c => (int?)c.Count).Sum() ?? 0;
            }
        }
        //获取音乐专辑购买的总金额
        public static decimal GetTotal(int userId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Carts.Where(c => c.UserId == userId).Select(c => (int?)c.Count * c.Albums.Price).Sum() ?? 0;
            }
        }       

    }
}